package example.posterity;

import com.scop.desensitize.encry.rreversible.symmetry.AbstractSymmetryEncry;
import com.scop.desensitize.exception.factory.FactoryException;
import com.scop.desensitize.exception.strategy.EncryStrategyException;
import com.scop.desensitize.factory.encry.EStrategy;
import com.scop.desensitize.factory.encry.irreversible.IrreverSibleFactory;
import com.scop.desensitize.factory.encry.rreversible.symmetry.SymmetryFactory;
import com.scop.desensitize.factory.posterity.StringDesFactory;
import com.scop.desensitize.posterity.AbstractPosterity;
import com.scop.desensitize.posterity.EPosterity;

/**
 * @desc 测试字符串的子孙后代类的功能拓展
 */
public class StringPosterityDemo {
    public static void main(String[] args) throws FactoryException, EncryStrategyException {
//        defaultMethod();
        method2();
    }

    private static void method2() throws FactoryException, EncryStrategyException {
        AbstractSymmetryEncry des3 = SymmetryFactory.create(EStrategy.DES3);

        System.out.println("通过工厂生产一个使用指定策略的字符串加密功能类");
        AbstractPosterity posterity = StringDesFactory.create(EPosterity.STRING,des3);
        Object enStr = posterity.encode("123456");
        Object deStr = posterity.decode(enStr);
        System.out.println("默认使用的是"+posterity.getEncry().getClass().getName()+"加密策略");
        System.out.println("加密："+enStr);
        System.out.println("解密："+deStr);
    }

    private static void defaultMethod() throws FactoryException, EncryStrategyException {
        System.out.println("通过工厂生产一个使用默认策略的字符串加密功能类");
        AbstractPosterity posterity = StringDesFactory.create(EPosterity.STRING);
        Object enStr = posterity.encode("123456");
        Object deStr = posterity.decode(enStr);
        System.out.println("默认使用的是"+posterity.getEncry().getClass().getName()+"加密策略");
        System.out.println("加密："+enStr);
        System.out.println("解密："+deStr);

        System.out.println("\n\n以下我们手动设定一个加密策略");
        posterity.setEncry(IrreverSibleFactory.create(EStrategy.MD5));
        enStr = posterity.encode("123456");
        System.out.println("使用的加密策略为："+posterity.getEncry().getClass().getName());
        System.out.println("加密："+enStr);
    }
}
